package com.kjc.demo.rcservice.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.TypeReference;
import com.kjc.demo.rcservice.bean.DocInfoVersionBean;
import com.kjc.demo.rcservice.dto.DocInfoVersionDTO;
import com.kjc.demo.rcservice.dto.req.DocInfoVersionReqDTO;
import com.kjc.demo.rcservice.dto.res.DocInfoVersionResDTO;
import com.kjc.demo.rcservice.service.DocInfoVersionService;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.common.entity.PaginationEntity;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

/**
 * @author DocInfoVersion
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-12-10
 */
 
@RefreshableRestController
@RequestMapping(value = "/docInfoVersion")
public class DocInfoVersionController {

    @Autowired
    private DocInfoVersionService docInfoVersionService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getDocInfoVersionPageList")
	public PaasBaseResponse<DocInfoVersionResDTO> getDocInfoVersionPageList(@RequestBody PaasBaseRequest<DocInfoVersionReqDTO> docInfoVersionReqDTO) {
    	DocInfoVersionDTO docInfoVersionDTO = docInfoVersionReqDTO.getBody().getDocInfoVersionDTO();
    	PageInfoDTO pageInfoDTO = docInfoVersionReqDTO.getBody().getPageInfo();
    	
		BasePage pageRequest = new BasePage();
		pageRequest.setPage(pageInfoDTO.getPage());
		pageRequest.setCount(pageInfoDTO.getPageSize());
		
		DocInfoVersionBean docInfoVersionBean = EntityDtoConvertUtil.convertToEntity(docInfoVersionDTO, DocInfoVersionBean.class);
		PaginationEntity<DocInfoVersionBean> page = docInfoVersionService.getPageDocInfoVersionList(pageRequest, docInfoVersionBean);
    	PageInfoDTO resPageInfoDTO = new PageInfoDTO();
    	DocInfoVersionResDTO docInfoVersionResDTO = new DocInfoVersionResDTO();
		resPageInfoDTO.setRowCount(page.getTotal());
		docInfoVersionResDTO.setPageInfo(resPageInfoDTO);
		List<DocInfoVersionBean> docInfoVersionBeanList = page.getRows();
		List<DocInfoVersionDTO> dtoList = new ArrayList<DocInfoVersionDTO>();
		dtoList = EntityDtoConvertUtil.convertToDTOList(docInfoVersionBeanList, new TypeReference<List<DocInfoVersionDTO>>() {
		});
		docInfoVersionResDTO.setDocInfoVersionDTOList(dtoList);
    	return new PaasBaseResponse<>(docInfoVersionResDTO);
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveDocInfoVersion")
	public PaasBaseResponse<DocInfoVersionResDTO> saveDocInfoVersion(@RequestBody PaasBaseRequest<DocInfoVersionReqDTO> docInfoVersionReqDTO) {
		DocInfoVersionDTO docInfoVersionDTO = docInfoVersionReqDTO.getBody().getDocInfoVersionDTO();
		DocInfoVersionBean docInfoVersionBean = EntityDtoConvertUtil.convertToEntity(docInfoVersionDTO, DocInfoVersionBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		docInfoVersionBean.setId(uuid);
		Boolean flag = docInfoVersionService.saveDocInfoVersion(docInfoVersionBean);
		docInfoVersionDTO.setId(uuid);
		DocInfoVersionResDTO docInfoVersionResDTO = new DocInfoVersionResDTO();
		docInfoVersionResDTO.setDocInfoVersionDTO(docInfoVersionDTO);
		return new PaasBaseResponse<>(docInfoVersionResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateDocInfoVersion")
	public PaasBaseResponse<DocInfoVersionResDTO> updateDocInfoVersion(@RequestBody PaasBaseRequest<DocInfoVersionReqDTO> docInfoVersionReqDTO) {
		DocInfoVersionDTO docInfoVersionDTO = docInfoVersionReqDTO.getBody().getDocInfoVersionDTO();
		DocInfoVersionBean docInfoVersionBean = EntityDtoConvertUtil.convertToEntity(docInfoVersionDTO, DocInfoVersionBean.class);
		Boolean flag = docInfoVersionService.updateDocInfoVersion(docInfoVersionBean);
		DocInfoVersionResDTO docInfoVersionResDTO = new DocInfoVersionResDTO();
		docInfoVersionResDTO.setDocInfoVersionDTO(docInfoVersionDTO);
		return new PaasBaseResponse<>(docInfoVersionResDTO);
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getDocInfoVersionDetail")
	public PaasBaseResponse<DocInfoVersionResDTO> getDocInfoVersionDetail(@RequestBody PaasBaseRequest<DocInfoVersionReqDTO> docInfoVersionReqDTO) {
		DocInfoVersionDTO docInfoVersionDTO = docInfoVersionReqDTO.getBody().getDocInfoVersionDTO();
		DocInfoVersionBean docInfoVersionBean = EntityDtoConvertUtil.convertToEntity(docInfoVersionDTO, DocInfoVersionBean.class);
		docInfoVersionBean = docInfoVersionService.getDocInfoVersionDetail(docInfoVersionBean.getId());
		DocInfoVersionResDTO docInfoVersionResDTO = new DocInfoVersionResDTO();
		docInfoVersionResDTO.setDocInfoVersionDTO(docInfoVersionDTO);
		return new PaasBaseResponse<>(docInfoVersionResDTO);
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteDocInfoVersion")
	public PaasBaseResponse<DocInfoVersionResDTO> deleteDocInfoVersion(@RequestBody PaasBaseRequest<DocInfoVersionReqDTO> docInfoVersionReqDTO) {
		DocInfoVersionDTO docInfoVersionDTO = docInfoVersionReqDTO.getBody().getDocInfoVersionDTO();
		DocInfoVersionBean docInfoVersionBean = EntityDtoConvertUtil.convertToEntity(docInfoVersionDTO, DocInfoVersionBean.class);
		Boolean flag = docInfoVersionService.deleteDocInfoVersion(docInfoVersionBean);
		DocInfoVersionResDTO docInfoVersionResDTO = new DocInfoVersionResDTO();
		docInfoVersionResDTO.setDocInfoVersionDTO(docInfoVersionDTO);
		return new PaasBaseResponse<>(docInfoVersionResDTO);
	}
}
